package com.wy.demo.jsonFormat.Jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import static org.junit.Assert.*;

public class JsonDemo {
    private ObjectMapper objectMapper;
    private User testUser;
    private File jsonFile;
    private File outputFile;

    @Before
    public void setUp() {
        objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        
        testUser = new User(1L, "张三", "zhangsan@example.com", new Date(), 
            Arrays.asList("读书", "游泳", "编程"),"1");
        
        jsonFile = new File("src/main/resources/user.json");
        outputFile = new File("src/main/resources/user.json");
    }

    @Test
    public void testObjectToJsonString() throws IOException {
        // 对象转 JSON 字符串
        String jsonString = objectMapper.writeValueAsString(testUser);
        System.out.println("对象转 JSON 字符串：");
        System.out.println(jsonString);
        
        assertNotNull(jsonString);
        assertTrue(jsonString.contains("张三"));
        assertTrue(jsonString.contains("email_address"));
    }

    @Test
    public void testJsonStringToObject() throws IOException {
        // 从文件读取 JSON 字符串
        String jsonString = objectMapper.writeValueAsString(testUser);
        
        // JSON 字符串转对象
        User parsedUser = objectMapper.readValue(jsonString, User.class);
        System.out.println("\nJSON 字符串转对象：");
        System.out.println("姓名：" + parsedUser.getName());
        System.out.println("邮箱：" + parsedUser.getEmail());
        
        assertEquals(testUser.getName(), parsedUser.getName());
        assertEquals(testUser.getEmail(), parsedUser.getEmail());
    }

    @Test
    public void testObjectToJsonFile() throws IOException {
        // 对象转 JSON 文件
        objectMapper.writeValue(outputFile, testUser);
        System.out.println("\n对象已写入文件：" + outputFile.getAbsolutePath());
        
        assertTrue(outputFile.exists());
        assertTrue(outputFile.length() > 0);
    }

    @Test
    public void testJsonFileToObject() throws IOException {
        // 从文件读取 JSON 到对象
        User fileUser = objectMapper.readValue(jsonFile, User.class);
        System.out.println("\n从文件读取 JSON 到对象：");
        System.out.println("姓名：" + fileUser.getName());
        System.out.println("爱好：" + fileUser.getHobbies());
        
        assertNotNull(fileUser);
        assertNotNull(fileUser.getName());
        assertNotNull(fileUser.getHobbies());
    }

    @Test
    public void testJsonNodeOperations() throws IOException {
        // 演示 JsonNode 操作
        String jsonString = objectMapper.writeValueAsString(testUser);
        JsonNode jsonNode = objectMapper.readTree(jsonString);
        
        System.out.println("\nJsonNode 操作：");
        System.out.println("获取 name 字段：" + jsonNode.get("name").asText());
        System.out.println("获取 hobbies 数组：" + jsonNode.get("hobbies"));
        
        assertTrue(jsonNode.has("name"));
        assertTrue(jsonNode.has("hobbies"));
        assertEquals("张三", jsonNode.get("name").asText());
    }

    @Test
    public void testJsonArrayOperations() throws IOException {
        // 创建用户数组
        User[] users = new User[]{
            new User(1L, "张三", "zhangsan@example.com", new Date(), Arrays.asList("读书", "游泳"),"1"),
            new User(2L, "李四", "lisi@example.com", new Date(), Arrays.asList("篮球", "足球"),"1")
        };
        
        // 数组转 JSON
        String jsonArray = objectMapper.writeValueAsString(users);
        System.out.println("\n数组转 JSON：");
        System.out.println(jsonArray);
        
        // JSON 转数组
        User[] parsedUsers = objectMapper.readValue(jsonArray, User[].class);
        System.out.println("\nJSON 转数组：");
        for (User user : parsedUsers) {
            System.out.println("姓名：" + user.getName());
        }
        
        assertEquals(2, parsedUsers.length);
        assertEquals("张三", parsedUsers[0].getName());
        assertEquals("李四", parsedUsers[1].getName());
    }
} 